package top.ljlgithub.api.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.ljlgithub.api.bean.*;
import top.ljlgithub.api.exception.ParamException;
import top.ljlgithub.api.mapper.OrderMapper;
import top.ljlgithub.api.service.OrderService;

import java.util.List;

/**
 * @ClassName
 * @Description TODO
 * @Author L1470
 * @Date 2021/5/7 16:39
 * @Version 1.0
 **/
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    OrderMapper orderMapper;
    @Override
    public OrderPageBean getOrderList(OrderParam orderParam) throws ParamException {
        if (orderParam.getPagesize() == null || orderParam.getPagenum() == null){
            throw new ParamException("参数错误");
        }
        int total = orderMapper.getTotal(orderParam.getQuery());
        int pagenum = orderParam.getPagenum();
        int startIndex = (pagenum - 1) * orderParam.getPagesize();
        List<Order> orderList = orderMapper.getOrderList(orderParam.getQuery(), startIndex, orderParam.getPagesize());
        OrderPageBean orderPageBean = new OrderPageBean(total, pagenum, orderList);
        return orderPageBean;
    }

    @Override
    public OrderGoods findOrderById(Integer id) {
        OrderGoods orderGoods = orderMapper.findOrderById(id);
        List<OrderGood> orderGoodList = orderMapper.getOrderGoodList(orderGoods.getOrder_id());
        orderGoods.setGoods(orderGoodList);
        return orderGoods;
    }

    @Override
    public void updateOrder(Order order) {
        orderMapper.updateOrder(order);
        orderMapper.findOrderById(order.getOrder_id());
    }
}
